/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Visual;

import Logica.Cliente;
import Logica.Vehiculo;
import Persistencia.exceptions.NonexistentEntityException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.DefaultListModel;
import javax.swing.JDesktopPane;

/**
 *
 * @author Pablo
 */
public class Vehiculos extends javax.swing.JInternalFrame {

    ControladoraVisual miVisual;
    JDesktopPane miEscritorio;
    Cliente unCliente;
    
    DefaultListModel modeloVehiculos = new DefaultListModel();
    
    
    /**
     * Creates new form Vehiculos
     */
    public Vehiculos(ControladoraVisual miVisual, JDesktopPane miEscritorio, Cliente unCliente) {
        initComponents();
        this.unCliente = unCliente;
        this.miVisual = miVisual;
        this.miEscritorio = miEscritorio;
        this.listVehiculos.setModel(modeloVehiculos);
        this.cargarVehiculos();
        this.txtCliente.setText(unCliente.getNombre()+" "+unCliente.getApellido());
        this.txtCliente.setEnabled(false);
        
    }
    
    public void cargarVehiculos(){
        List<Vehiculo> vehiculos = miVisual.dameVehiculos();
        for(Vehiculo miVehiculo:vehiculos){
            //System.out.println("vehiculo_dni: "+miVehiculo.getUnCliente().getNroDoc()+" unCliente: "+unCliente.getNroDoc());
            double dniCliente = this.unCliente.getNroDoc();
            double dniLista = miVehiculo.getUnCliente().getNroDoc(); 
            
            if(dniLista == dniCliente) 
            
                this.modeloVehiculos.addElement(miVehiculo);
            
        }
        
    }
    public void referenciarCliente() throws NonexistentEntityException, Exception{  // referencia el cliente al vehiculo agregado (si se agrega un nuevo vehiculo)
        int dimension = this.listVehiculos.getModel().getSize();
        
        
        Vehiculo nuevoVehiculo = (Vehiculo) this.modeloVehiculos.getElementAt(dimension - 1);
        System.out.println(nuevoVehiculo);
        nuevoVehiculo.setUnCliente(unCliente);
        miVisual.editarVehiculo(nuevoVehiculo);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        listVehiculos = new javax.swing.JList();
        jLabel1 = new javax.swing.JLabel();
        txtCliente = new javax.swing.JTextField();
        btnSalir = new javax.swing.JButton();
        btnSiguiente = new javax.swing.JButton();
        btnNuevoVehiculo = new javax.swing.JButton();
        jSeparator1 = new javax.swing.JSeparator();

        jScrollPane1.setViewportView(listVehiculos);

        jLabel1.setText("Vehiculos del Cliente");

        btnSalir.setText("Salir");
        btnSalir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSalirActionPerformed(evt);
            }
        });

        btnSiguiente.setText("Siguiente");
        btnSiguiente.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSiguienteActionPerformed(evt);
            }
        });

        btnNuevoVehiculo.setText("Nuevo Vehiculo");
        btnNuevoVehiculo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnNuevoVehiculoActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jSeparator1)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txtCliente))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addComponent(btnSalir)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 100, Short.MAX_VALUE)
                        .addComponent(btnSiguiente))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addComponent(btnNuevoVehiculo)
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(txtCliente, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(btnNuevoVehiculo)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 23, Short.MAX_VALUE)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnSalir)
                    .addComponent(btnSiguiente))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnSalirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSalirActionPerformed
        this.dispose();
    }//GEN-LAST:event_btnSalirActionPerformed

    private void btnSiguienteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSiguienteActionPerformed
        try {
            this.referenciarCliente();
        } catch (NonexistentEntityException ex) {
            Logger.getLogger(Vehiculos.class.getName()).log(Level.SEVERE, null, ex);
        } catch (Exception ex) {
            Logger.getLogger(Vehiculos.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        Vehiculo unVehiculo = (Vehiculo) listVehiculos.getSelectedValue();
        frmAltaTurno miTurno = new frmAltaTurno(miVisual,miEscritorio,unCliente,unVehiculo);
        miEscritorio.add(miTurno);
        miTurno.setVisible(true);
    }//GEN-LAST:event_btnSiguienteActionPerformed

    private void btnNuevoVehiculoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnNuevoVehiculoActionPerformed
        frmAltaVehiculo unVehiculo = new frmAltaVehiculo(miVisual, miEscritorio, modeloVehiculos);
        miEscritorio.add(unVehiculo);
        unVehiculo.setMaximizable(true);
        unVehiculo.setClosable(true);
        unVehiculo.setVisible(true);
    }//GEN-LAST:event_btnNuevoVehiculoActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnNuevoVehiculo;
    private javax.swing.JButton btnSalir;
    private javax.swing.JButton btnSiguiente;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JList listVehiculos;
    private javax.swing.JTextField txtCliente;
    // End of variables declaration//GEN-END:variables
}
